﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FilStartUps.FILAPI;
using System.Web.Security;
using FilStartUps.DAL;
using FilStartUps.Helper;

namespace FilStartUps.Controls
{
    public partial class FilStartUps_PageNav : System.Web.UI.UserControl
    {
        //GETPAGE NAV
        public void GetPageNav()
        {
            try
            {
                var pagenav = FilStartUpsAPI.GetPageNav();
                rp_FilStartUpsNav.DataSource = pagenav;
                rp_FilStartUpsNav.DataBind();
            }
            catch
            {

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (Context.User.Identity.IsAuthenticated)
                {
                    lbLgout.Visible = true;
                    lbProfile.Visible = true;
                    lbRegister.Visible = false;
                    lbRegister.Enabled = false;
                    lblogin.Enabled = false;
                    lblogin.Visible = false;
                    using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
                    {
                        var user = db_connect.Members.Where(m => m.emailaddress.Equals(Context.User.Identity.Name)).SingleOrDefault();
                        //RoleidFK == 2 Member; RoleidFK ==1 Moderator
                        if (user.roleidFK == 2)
                        {
                            GetPageNav();
                            lnadmin.Visible = false;
                            lnadmin.Enabled = false;
                        }
                        else
                        {
                            GetPageNav();
                            lnadmin.Visible = true;
                            lnadmin.Enabled = true;
                        }
                    }
                }
                else
                {
                    GetPageNav();
                }
            }
        }

        //Response.Redirect("Page.aspx?pageidPK="+ lbPageidPK.text)
        //Page.aspx:+ "&pagename=" + linkPageName.Text
        //FilStartUps_Category
        protected void rp_FilStartUpsNav_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "SelectRow")
            {
                LinkButton linkPageName = (LinkButton)rp_FilStartUpsNav.Items[e.Item.ItemIndex].FindControl("pagename");
                Label lbPageidPK = (Label)rp_FilStartUpsNav.Items[e.Item.ItemIndex].FindControl("pageidPK");
                Response.Redirect("~/Content.aspx?pg=" + lbPageidPK.Text);
            }
        }

        protected void lbHome_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }

        protected void lkForum_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Forum/ForumDisplay.aspx");
        }

        protected void lbLgout_Click(object sender, EventArgs e)
        {
            using (FilStartUpsEntities db_connect = new FilStartUpsEntities())
            {
                var member = db_connect.Members.Where(m => m.emailaddress.Equals(Context.User.Identity.Name)).SingleOrDefault();
                if (member != null) SessionHelper.AbandonSession(member.memberidPK);
            }

            Session.Abandon();
            FormsAuthentication.SignOut();

            // clear authentication cookie
            HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
            cookie1.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie1);

            // clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
            HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
            cookie2.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie2);


            Response.Redirect("~/Home.aspx");
        }

        protected void lbProfile_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Account/MemberProfile.aspx");
        }

        protected void lblogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Account/Login.aspx");
        }

        protected void lbRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Account/Registration.aspx");
        }

    }
}